#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <vector>
// 可以给线程传递多个参数，甚至方法了
class ThreadData
{
public:
    int Excute()
    {
        return x + y;
    }

public:
    std::string name;
    int x;
    int y;
    // other
};

class ThreadResult
{
public:
    std::string print()
    {
        return std::to_string(x) + "+" + std::to_string(y) + "=" + std::to_string(result);
    }

public:
    int x;
    int y;
    int result;
};
void PrintToHex(pthread_t tid)
{
    char buffer[128];
    snprintf(buffer, sizeof(buffer), "0x%lx", tid);
    std::cout << buffer << std::endl;
}
// void *ThreadRun(void *args)
// {
//     // int cnt=5;
//     // while(cnt)
//     // {
//     //     std::cout<<"new thead run cnt: "<<cnt--<<std::endl;
//     //     sleep(1);
//     // }

//     ThreadData *td = static_cast<ThreadData *>(args);
//     // std::cout << td->name << std::endl;
//     ThreadResult *tr = new ThreadResult;
//     tr->result = td->Excute();
//     tr->x = td->x;
//     tr->y = td->y;
//     delete td;

//     return (void *)tr;
// }

void *ThreadRun(void *args)
{
    // pthread_detach(pthread_self());
    // pthread_cancel(pthread_self());

    // std::cout<<(const char *)args<<std::endl;
    int cnt = 5;
    while (true)
    {
        std::cout << (char *)args << " is running " << std::endl;
        sleep(1);
    }
    return nullptr;
}
void *ThreadRun1(void *args)
{
    // std::cout<<(const char *)args<<std::endl;
    while (true)
    {
        std::cout << (char *)args << " is running " << std::endl;
        sleep(1);
        // char *p=nullptr;//一个线程被终止退出时是整个进程退出
        // *p=100;
        sleep(5);
        break;
    }
    // exit(1);//用来终止一个进程的，不能用来终止线程
    pthread_exit(args);
    sleep(10);
    return nullptr;
}
int main()
{
    // 创建多线程
    std::vector<pthread_t> tids;
    for (int i = 0; i < 5; i++)
    {
        pthread_t tid;
        // char buffer[128];//这里不能这样，这是一个局部变量，下一次就找不到了
        char *buffer = new char[128];
        int n = snprintf(buffer, 128, "thread %d", i + 1);
        // if(i==4)
        // pthread_create(&tid,nullptr,ThreadRun1,(void*)buffer);
        // else
        pthread_create(&tid, nullptr, ThreadRun, (void *)buffer);
        tids.emplace_back(tid);
    }
    // for(auto tid:tids)
    // pthread_cancel(tid);

    // for(auto tid : tids)
    // pthread_detach(tid);
    sleep(3);
    for (auto tid : tids)
    {
        // pthread_cancel(tid);

        void *ret = nullptr;
        int n = pthread_join(tid, (void **)&ret);
        std::cout << "new thread quit code " << (long long int)ret << std::endl;
    }

    // pthread_t tid;
    // // PrintToHex(tid);//tid是一个巨大的数组，是一个虚拟地址
    // ThreadData *td = new ThreadData;
    // td->name = "thread-1";
    // td->x = 10;
    // td->y = 20;
    // int n = pthread_create(&tid, nullptr, ThreadRun, (void *)td);
    // if (n != 0)
    // {
    //     std::cerr << "pthread_create fail" << ::std::endl;
    //     exit(-1);
    // }
    // ThreadResult *retval = nullptr;
    // int rid = pthread_join(tid, (void **)&retval);
    // if (n == 0)
    // {

    //     std::cout<< " thread wait success return " <<retval->print()<< std::endl;
    // }
    // delete retval;
    return 0;
}
